package com.example.smartcity4.ui.metro;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.javaBean.MetroDalBean;
import com.example.smartcity4.reteofi.SmartService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MetroDalActivity extends AppCompatActivity {
    private TextView title;
    private ImageView left;
    private TextView lineName;
    private TextView lineStart;
    private TextView lineTime;
    private TextView lineStation;
    private TextView lineRetime;
    private TextView lineKm;
    private GridView grid;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metro_dal);
        initView();
    }

    private void initView() {


        title = findViewById(R.id.title);
        left = findViewById(R.id.left);
        lineName = findViewById(R.id.line_name);
        lineStart = findViewById(R.id.line_start);
        lineTime = findViewById(R.id.line_time);
        lineStation = findViewById(R.id.line_station);
        lineRetime = findViewById(R.id.line_retime);
        lineKm = findViewById(R.id.line_km);
        grid = findViewById(R.id.grid);
        initData();

    }

    private void initData() {
        int id = getIntent().getIntExtra("id", 0);

        System.out.println("````````````````"+id);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService service = retrofit.create(SmartService.class);

        Call<MetroDalBean> call = service.get_line(id);
        call.enqueue(new Callback<MetroDalBean>() {
            @Override
            public void onResponse(Call<MetroDalBean> call, Response<MetroDalBean> response) {
//                Toast.makeText(getApplicationContext(), ""+response.body().msg, Toast.LENGTH_SHORT).show();
                name = response.body().data.runStationsName;
                lineName.setText(response.body().data.name);
                lineStart.setText(response.body().data.first + "->" + response.body().data.end);
                lineTime.setText("始:" + response.body().data.startTime + "->+ 末:" + response.body().data.endTime);
                lineStation.setText(response.body().data.runStationsName);
                lineRetime.setText(response.body().data.remainingTime + "分钟");
                lineKm.setText(response.body().data.km + "km");
                grid.setAdapter(new lineAdapter(getApplicationContext(), response.body().data.metroStepList));
                ViewGroup.LayoutParams layoutParams = grid.getLayoutParams();
                grid.setNumColumns(response.body().data.metroStepList.size());
                layoutParams.width = 120 * response.body().data.metroStepList.size();
                grid.setLayoutParams(layoutParams);

            }

            @Override
            public void onFailure(Call<MetroDalBean> call, Throwable throwable) {

            }
        });

    }

    public class lineAdapter extends BaseAdapter {
        Context context;
        List<MetroDalBean.Data.MetroStepList> list;

        public lineAdapter(Context context, List<MetroDalBean.Data.MetroStepList> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(R.layout.line_grid_item, parent, false);
            ImageView imageView = convertView.findViewById(R.id.image);
            TextView textView = convertView.findViewById(R.id.name);
            textView.setText(list.get(position).name);
            if (list.get(position).name.equals(name)) {
                Glide.with(context).load(getResources().getDrawable(R.drawable.shape1, null)).into(imageView);

            } else {
                Glide.with(context).load(getResources().getDrawable(R.drawable.shape2, null)).into(imageView);
            }
            return convertView;
        }
    }
}